local yishijielvzhe = fk.CreateSkill{
  name = "ym_water__yishijielvzhe",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["ym_water__yishijielvzhe"] = "异世界旅者",
  [":ym_water__yishijielvzhe"] = "锁定技，此武将牌不会出现在选将框。若场上有<a href = 'ym_water__miHoYoGeneral'><font color='#256781'>米游武将牌</font></a>，玩家选择非米游武将牌作为武将时，其可以将之替换为此武将并立刻登场。",
  ["ym_water__miHoYoGeneral"] = "因技术限制，目前只能检索部分扩展的势力，或者武将所在扩展包名、前缀、画师、cv、称号之类信息中有“米哈游/miHoYo”或对应游戏名的武将。",

  ["#ym_water__yishijielvzhe-invoke"] = "异世界旅者：是否将武将替换为Lumine·荧？",

  ["$ym_water__yishijielvzhe1"] = "这次又会遇见什么？",
  ["$ym_water__yishijielvzhe2"] = "要做的事，还有很多。",
  ["$ym_water__yishijielvzhe3"] = "世界，充满未解之谜……",
}

-- 获取一名角色是否使用了米游角色作为武将牌
---@param player ServerPlayer
local function isMiHoYoGeneral(player)
  local general = player.general
  local deputyGeneral = player.deputyGeneral
  --从主将势力来看
  if general and Fk.generals[general] and table.contains({"mondstadt", "liyue", "inazuma", "sumeru", "fontaine", "natlan", "snezhnaya", "void", "xianzhou", "belobog", "starrail", "hertass", "tey"},string.lower(Fk.generals[general].kingdom)) then
    return true
  end
  --从副将势力来看
  if deputyGeneral and Fk.generals[deputyGeneral] and table.contains({"mondstadt", "liyue", "inazuma", "sumeru", "fontaine", "natlan", "snezhnaya", "void", "xianzhou", "belobog", "starrail", "hertass", "tey"},string.lower(Fk.generals[deputyGeneral].kingdom)) then
    return true
  end
  local subExtension = {"提瓦特","原神","崩坏","米哈游","miHoYo","绝区零","崩坏","崩铁","星穹铁道","星铁","米忽悠","tey","六分街"}
  --从主将扩展包名来看
  for _, subExtensionName in ipairs(subExtension) do
    local str = Fk.generals[general].package.name
    if string.lower(str):startsWith(string.lower(subExtensionName)) then
      return true
    end
  end
  --从副将扩展包名来看
  if Fk.generals[deputyGeneral] then
    for _, subExtensionName in ipairs(subExtension) do
      local str = Fk.generals[deputyGeneral].package.name
      if string.lower(str):startsWith(string.lower(subExtensionName)) then
        return true
      end
    end
  end
  --从主将前缀来看
  for _, subExtensionName in ipairs(subExtension) do
    local subGeneralName = general:match("(.-)__") or general
    local str = Fk:translate(subGeneralName,"zh_CN")
    if string.lower(str):startsWith(string.lower(subExtensionName)) then
      return true
    end
  end
  --从副将前缀来看
  if Fk.generals[deputyGeneral] then
    for _, subExtensionName in ipairs(subExtension) do
      local subGeneralName = deputyGeneral:match("(.-)__") or deputyGeneral
      local str = Fk:translate(subGeneralName,"zh_CN")
      if string.lower(str):startsWith(string.lower(subExtensionName)) then
        return true
      end
    end
  end
  --从主将画师来看
  for _, subExtensionName in ipairs(subExtension) do
    local str = Fk:translate("illustrator:"..general,"zh_CN")
    if string.lower(str):startsWith(string.lower(subExtensionName)) then
      return true
    end
  end
  --从副将画师来看
  if Fk.generals[deputyGeneral] then
    for _, subExtensionName in ipairs(subExtension) do
      local str = Fk:translate("illustrator:"..deputyGeneral,"zh_CN")
      if string.lower(str):startsWith(string.lower(subExtensionName)) then
        return true
      end
    end
  end
  --从主将cv来看
  for _, subExtensionName in ipairs(subExtension) do
    local str = Fk:translate("cv:"..general,"zh_CN")
    if string.lower(str):startsWith(string.lower(subExtensionName)) then
      return true
    end
  end
  --从副将cv来看
  if Fk.generals[deputyGeneral] then
    for _, subExtensionName in ipairs(subExtension) do
      local str = Fk:translate("cv:"..deputyGeneral,"zh_CN")
      if string.lower(str):startsWith(string.lower(subExtensionName)) then
        return true
      end
    end
  end
  --从主将称号来看
  for _, subExtensionName in ipairs(subExtension) do
    local str = Fk:translate("#"..general,"zh_CN")
    if string.lower(str):startsWith(string.lower(subExtensionName)) then
      return true
    end
  end
  --从副将称号来看
  if Fk.generals[deputyGeneral] then
    for _, subExtensionName in ipairs(subExtension) do
      local str = Fk:translate("#"..deputyGeneral,"zh_CN")
      if string.lower(str):startsWith(string.lower(subExtensionName)) then
        return true
      end
    end
  end
end

yishijielvzhe:addEffect(fk.GamePrepared, {
  global = true,
  can_trigger = function (self, event, target, player, data)
    return (not table.contains(Fk:currentRoom().disabled_generals, "ym_water__ying") or Fk.skills["hanqing__weiqianlong"]) and
      table.find(player.room.players, function (s)
        return isMiHoYoGeneral(s)
      end) and not isMiHoYoGeneral(player) and
      not table.find(player.room.players, function (p)
        return p.general == "ym_water__ying" or p.deputyGeneral == "ym_water__ying"
      end)
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = yishijielvzhe.name,
      prompt = "#ym_water__yishijielvzhe-invoke",
    })
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:changeHero(player, "ym_water__ying", true, false, true, true, true)

    if not table.find(room.alive_players,function (cp)
      local hidden_general = cp:getMark("__hidden_general")
      if hidden_general == 0 then
        return cp:isMale()
      else
        return Fk.generals[hidden_general] and (Fk.generals[hidden_general].gender == General.Male or Fk.generals[hidden_general].gender == General.Bigender)
      end
    end) then
      player:chat("<font color='orange'>全是女同，太爽了</font>")
      room:addTableMarkIfNeed(player,"ym_water_lieshijiexiehou_jieyin","性别")
    end
  end,
})

return yishijielvzhe
